<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/27 17:45
 */

namespace app\admin\model;


class SystemTheme extends DataBase
{
    protected $name = 'system_theme';
    protected $autoWriteTimestamp = true;

    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getIsFreeAttr($value)
    {
        $status = [ 1 => '免费', 2 => '收费'];
        return $status[$value];
    }

    public function getIsDefaultAttr($value)
    {
        $status = [ 1 => '默认', 2 => '非默认'];
        return $status[$value];
    }

    /**
     * @param $page_config
     * @param $map
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        $order = input('sort','system_theme_id').' '.input('order','desc');
        return $this
            ->order($order)
            ->paginate($page_config);
    }


    public function systemAdd()
    {
       // halt($_FILES);
        $newData['theme_url'] = oosUpload($_FILES['theme_url'],'ss-theme','oss-cn-beijing.aliyuncs.com');
        $newData['preview_url'] = oosUpload($_FILES['preview_url'],'ss-theme','oss-cn-beijing.aliyuncs.com');
        $newData['theme_name'] = input('post.theme_name');
        $newData['is_default'] = input('post.is_default');
        $newData['status'] = input('post.status');
        $newData['is_free'] = input('post.is_free');

        $re = $this->where('theme_name','=',$newData['theme_name'])->find();
        if($re) return false;

        $res = $this->save($newData);
        $system_theme_id = $this->where('theme_name','=',$newData['theme_name'])->value('system_theme_id');
        if ($_POST['is_free'] == 2) {
            $a = count($_POST['days']);
            for ($i = 0; $i < $a; $i++) {
                if (!empty($_POST['days'][$i]) && !empty($_POST['price'][$i])) {
                    $info = array(
                        'theme_id' =>$system_theme_id,
                        'type' => $_POST['type'],
                        'price' => $_POST['price'][$i],
                        'days' => $_POST['days'][$i],
                    );
                    $ree = ThemePrice::create($info);
                }
            }
            if($res && $ree){
                return true;
            }else{
                return false;
            }
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }
}